<template>
  <div class="home">
    <div class="box">
      <div class="src"><img alt="Vue logo" src="../assets/logo.png" /></div>
      <div class="box-fat">
        <div class="box-sub">
          <p><span>*</span>账号</p>
          <input v-model="input1" type="text" name="" id="" />
        </div>
        <div class="box-sub">
          <p><span>*</span>密码</p>
          <input v-model="input2" type="text" name="" id="" />
        </div>
      </div>
      <div class="box-sub2">
        <el-button type="primary" @click="but()">登录</el-button>
        <el-button type="info">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
// @ is an alias to /src
import { postLogin,getMenus } from "../tools/ajax";
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { ElMessage } from 'element-plus'
const router = useRouter(); // 获取路由
const store = useStore(); // 获取状态管理仓库
const input1 = ref("");
const input2 = ref("");
function but() {
  console.log(this.input1);
  console.log(this.input2);

  postLogin({ username: this.input1, password: this.input2 }).then((res) => {
    console.log(res);
    if (res.meta.status == 200) {
      console.log(1111);
      ElMessage.success(res.meta.msg)
      store.commit("setToken", res.data.token);
      store.commit("setName", res.data.username);
      setTimeout(() => {
        router.push("/Home");
      }, 1000);
      // 登录成功, 拿token请求左侧菜单列表
      getMenus().then(res2 => {
          store.commit("setMenus", res2.data)
          console.log(res2.data);
          // 初始化动态路由
          router.init()
        })
    } else {
      ElMessage.error(res.meta.msg)
    }
  });
}
</script>
<style lang="less" scoped>
.box {
  width: 500px;
  height: 300px;
  margin: 50px auto;
  background-color: rgb(96, 194, 250);
  position: relative;
  .src {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    overflow: hidden;
    position: absolute;
    top: -30px;
    left: 200px;
    background-color: white;
    img {
      width: 80px;
      height: 80px;
    }
  }
  .box-fat {
    padding-top: 50px;
    .box-sub {
      display: flex;
      align-items: center;
      justify-content: space-evenly;
      span {
        color: red;
      }
      input {
        width: 80%;
        height: 30px;
      }
    }
  }
  .box-sub2 {
    margin-top: 30px;
  }
}
</style>